package com.example.lzl.jisuanqi;

import android.content.Intent;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //定义按钮
    Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
    Button btnjia,btnjian,btnchu,btncheng,btndeng;
    Button btnclear,btndel,btndian;
    EditText et_input;//输入显示
    boolean clearflag=false;//清空标示
    Button btn_zhuan;//跳转到进制转换页面
    double result=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化按钮
        btn0=(Button) (findViewById(R.id.button_0));
        btn1=(Button) (findViewById(R.id.button_1));
        btn2=(Button) (findViewById(R.id.button_2));
        btn3=(Button) (findViewById(R.id.button_3));
        btn4=(Button) (findViewById(R.id.button_4));
        btn5=(Button) (findViewById(R.id.button_5));
        btn6=(Button) (findViewById(R.id.button_6));
        btn7=(Button) (findViewById(R.id.button_7));
        btn8=(Button) (findViewById(R.id.button_8));
        btn9=(Button) (findViewById(R.id.button_9));
        btnjia=(Button) (findViewById(R.id.button_jia));
        btnjian=(Button) (findViewById(R.id.button_jian));
        btnchu=(Button) (findViewById(R.id.button_chu));
        btncheng=(Button) (findViewById(R.id.button_cheng));
        btndeng=(Button) (findViewById(R.id.button_deng));
        btnclear=(Button) (findViewById(R.id.button_clear));
        btndel=(Button) (findViewById(R.id.button_del));
        btndian=(Button) (findViewById(R.id.button_dian));
        et_input=(EditText) (findViewById(R.id.et_input));
        btn_zhuan= (Button) findViewById(R.id.button_zhuan);

        //按钮的点击事件
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnjia.setOnClickListener(this);
        btnjian.setOnClickListener(this);
        btncheng.setOnClickListener(this);
        btnchu.setOnClickListener(this);
        btndeng.setOnClickListener(this);
        btndian.setOnClickListener(this);
        btnclear.setOnClickListener(this);
        btndel.setOnClickListener(this);
        btn_zhuan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String str=et_input.getText().toString();
        switch (v.getId()){
            case R.id.button_0:
            case R.id.button_1:
            case R.id.button_2:
            case R.id.button_3:
            case R.id.button_4:
            case R.id.button_5:
            case R.id.button_6:
            case R.id.button_7:
            case R.id.button_8:
            case R.id.button_9:
            case R.id.button_dian:
                if(clearflag){
                    clearflag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str+((Button)v).getText());
                break;
            case R.id.button_jia:
            case R.id.button_jian:
            case R.id.button_cheng:
            case R.id.button_chu:
                if(clearflag){
                    clearflag=false;
                    str="";
                    et_input.setText("");
                }
                if(result!=0){
                    str=result+"";
                }
                et_input.setText(str+" "+((Button)v).getText()+" ");
                break;
            case R.id.button_deng:
                getResult();
                break;
            case R.id.button_clear:
                clearflag=false;
                str="";
                result=0;
                et_input.setText("");
                break;
            case R.id.button_del:
                if(clearflag){
                    clearflag=false;
                    str="";
                    et_input.setText("");
                }else if(str!=null&&!str.equals("")){
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.button_zhuan:
                Intent intent=new Intent(MainActivity.this,jinzhiActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
        }
    }

    private void getResult() {
        String exp=et_input.getText().toString();
        if(exp==null||exp.equals("")){
            return;
        }
        if(!exp.contains(" ")){
            return;
        }
        if(clearflag){
            clearflag=false;
            return;
        }
        clearflag=true;
        //double result = 0;
        String s1=exp.substring(0,exp.indexOf(" "));
        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
        String s2=exp.substring(exp.indexOf(" ")+3);
        if(!s1.equals("")&&!s2.equals("")){
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                result=d1+d2;
            }else if(op.equals("-")){
                result=d1-d2;
            }else if(op.equals("*")){
                result=d1*d2;
            }else if(op.equals("/")){
                if(d2==0){
                    result=0;
                }else{
                    result=d1/d2;
                }
            }
            if(!s1.contains(".")&&!s2.contains(".")){
                int r=(int)result;
                et_input.setText(r+"");

            }else{
                et_input.setText(result+"");
            }
        }else if(!s1.equals("")&&s2.equals("")){
            et_input.setText(exp);
        }else if(s1.equals("")&&!s2.equals("")){
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                result=0+d2;
            }else if(op.equals("-")){
                result=0-d2;
            }else if(op.equals("*")){
                result=0;
            }else if(op.equals("/")){
                result=0;
            }
            if(s2.contains(".")){
                int r=(int)result;
                et_input.setText(r+"");
            }else{
                et_input.setText(result+"");
            }
        }else{
            et_input.setText("");
        }
    }

}
